package com.lizhongbao.cloud.db.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description: mybatis-plus公共字段自动填充，https://baomidou.oschina.io/mybatis-plus-doc/#/auto-fill
 * @Author: lizhongbao
 * @Date: 2019-9-19 11:06:02
 */
@Component
public class MetaObjectHandlerConfig implements MetaObjectHandler {

    public static final String TENANT_ID = "tenantId";
    public static final String CREATED_AT = "createdAt";
    public static final String UPDATED_AT = "updatedAt";
    public static final String CREATED_BY = "createdBy";
    public static final String UPDATED_BY = "updatedBy";
    public static final String VERSION = "version";
    public static final String DR = "dr";

    /**
     * 插入方法实体填充
     *
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        setFieldValByName(TENANT_ID, "lzb", metaObject);
        setFieldValByName(VERSION, 1, metaObject);
        setFieldValByName(DR, 0, metaObject);
        setFieldValByName(CREATED_AT, new Date(), metaObject);
        setFieldValByName(CREATED_BY, "admin", metaObject);
    }

    /**
     * 更新方法实体填充
     *
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName(UPDATED_AT, new Date(), metaObject);
        setFieldValByName(UPDATED_BY, "admin", metaObject);
    }
}